﻿var db = null; //new Map();

/*
功能:存储即时数据
memo:不同的port对应不同的应用,8051端口 8052端口
*/
function device_storage(port, deviceid, content) {
    if (db == null)
        db = new Map();
    var dc = db.get(port);

    if (dc == null) {
        //console.log("new map child");
        //console.log(port,deviceid,content);
        dc = {};
        dc[deviceid] = content;
        db.set(port, dc);
    }
    else {
        dc[deviceid] = content;
    }
}


function device_get_bydevice(port, deviceid) {
    if (db == null)
        return null;
    var temp = db.get(port);
    if (temp == null)
        return null;
    return temp[deviceid];
}



function device_get_by_port(port) {
    if (db == null)
        return null;
    var temp = db.get(port);
    if (temp == null)
        return null;
    return temp;
}
function device_del_bydevice(port, deviceid) {
    if (db == null)
        return null;
    var temp = db.get(port);
    if (temp == null)
        return null;
    delete temp[deviceid];
    //db.delete(deviceid);
}


/*
//功能:         存储即时数据       
//data:         必须要保存到数据库的数据,
//data_ext:     只需要保存到即时数据里的数据
//data_callback 保存到数据库的回调函数
*/
function tcpcallback(port, data, data_ext, data_callback) {
    if(data == null && data_ext == null)
        return;
    var deviceid = null;
    var obj = null;
    deviceid = data.deviceid;
    if (data != null) {
        obj = device_get_bydevice(port, deviceid);

        if(data.name!=null){ //数组,根据名称不同,存入数组
            if (obj == null) {
                obj = [];
                var n = {};
                Object.assign(n, data);
                obj.push(n);
            }
            else {//已经存在
                var ifind = false;
                if (Array.prototype.isPrototypeOf(obj) == true) { //是数组才对
                    for (var i = 0; i < obj.length; i++) {
                        var obj_ = obj[i];
                        if (obj_.name != null && obj_.name == data.name) {
                            ifind = true;
                            obj[i] = data;
                            break;
                        }
                    }
                    if (ifind == false) {
                        obj.push(data);
                    }
                }
            }//if(Array
        }
        else { //存储对象
            if (obj == null)
                obj = {};
            for (var key in data) {
              obj[key] = data[key];
           }
        }
        device_storage(port, deviceid, obj);
    }
    if (data_ext != null){
        deviceid = data_ext.deviceid;
        obj = device_get_bydevice(port,deviceid);
        if (obj == null)
            return;
        if (Array.prototype.isPrototypeOf(obj) == true) { //如果是数组
            for (var i = 0; i < obj.length; i++) {
                var obj_ = obj[i];
                for (var key in data_ext) {
                    obj_[key] = data_ext[key];
                }
            }
        }
        else {
            for (var key in data_ext) {
                obj[key] = data_ext[key];
            }
        }
        for (var key in data_ext) {
            obj[key] = data_ext[key];
        }
        device_storage(port, deviceid, obj);
    }
    //存储到内存
 
    //存储到数据库
    if (data_callback != null && data != null)
        data_callback(port, data);
}



function get_port_device_data(port, deviceids) {
    if (Array.prototype.isPrototypeOf(deviceids) != true) { //如果不是数组
        console.log("is not an array");
        return null;
    }
    // console.log(port,deviceids);
     var obj = [];
    for(var i =0;i< deviceids.length;i++){
        var o = device_get_bydevice(port, deviceids[i]);
        if (o != null) {
            if (Array.prototype.isPrototypeOf(o) == true) { //如果不是数组
                for (var j = 0; j < o.length; j++) {
                    obj.push(o[j]);
                }
            }
            else
                obj.push(o);

        }

    }
    return obj;
}


function get_port_device_data_1(port, deviceid) {
    //var obj = [];
    //for (var i = 0; i < deviceids.length; i++) {
    return device_get_bydevice(port, deviceid);
}

exports.tcpcallback = tcpcallback;
exports.device_del_bydevice = device_del_bydevice;
exports.get_port_device_data = get_port_device_data;
exports.get_port_device_data_1 = get_port_device_data_1;
//cache 测试代码
/*
var obj1 = {temp:"1",press:"2",time:"2017",deviceid:"01010101"};
var obj2 = { temp: "1", press: "2", time: "2017", deviceid: "01010102" };
var obj3 = { temp: "1", press: "2", time: "2017", deviceid: "01010103" };
device_storage("test1", "01010101", obj1);
device_storage("test2", "01010102", obj2);
device_storage("test1", "01010103", obj3);

//console.log(device_get_bydevice("test2", "01010102"));
var temp = device_get_bycompany("test1");
for (var prop in temp) {
    console.log(temp[prop]);
}
var obj = JSON.stringify(temp);
console.log(obj);
*/


//buffer 测试代码
//console.log(config.get('redis'));

//var buf = new Buffer([0x69, 0x00, 0x01, 0x00, 0x01, 0x03, 0x00, 0x31, 0x00, 0x01, 0xb7, 0x84, 0x16]);
//var buf2 = new Buffer([0x69, 0x00, 0x01, 0x00, 0x01, 0x04, 0x08, 0x46, 0x7A, 0x0B, 0xE8, 0x41, 0xF1, 0x00, 0x00, 0xB2, 0x87, 0x16]);

//var v_temp = buf2.readFloatBE(7);
//var v_press = buf2.readFloatBE(11);

//var v_deviceid = buf.toString('hex', 1, 5);

//const buffer = Buffer.allocUnsafe(16);
//buffer[0] = 0x01;
//buffer[1] = 0x02;


//buf2.copy(buf2, 0, 5, 18);
//console.log(buf2.toString('hex'));
//console.log(v_press);